import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/HomeView"
  },
  {
    path: '/HomeView',
    name: "HomeView",
    component: HomeView,
    meta: {/* 路由缓存 */ keep: true }
  },
  {
    path: '/CommunityView',
    name: "CommunityView",
    component: () => import("../views/CommunityView.vue"),
    meta: {/* 路由缓存 */ keep: true }
  },
  {
    path: '/FoundView',
    name: "FoundView",
    component: () => import("../views/FoundView.vue"),
    meta: {/* 路由缓存 */ keep: true }
  },
  {
    path: '/SpaceView',
    name: "SpaceView",
    component: () => import("../views/SpaceView.vue"),
    // meta: {/* 路由缓存 */ keep: true }
  },
  {
    path: '/PostView',
    name: "PostView",
    component: () => import("../views/PostView.vue")
  },
  {
    path: '/PostDetails',
    name: "PostDetails",
    component: () => import("../views/PostDetails.vue")
  },
  {
    path: '/EditView',
    name: "EditView",
    component: () => import("../views/EditView.vue"),
    // meta: {/* 路由缓存 */ keep: true }
  },
  {
    path: '/MsgView',
    name: "MsgView",
    component: () => import("../views/MsgView.vue")
  },
  {
    path: '/ReplyView',
    name: "ReplyView",
    component: () => import("../views/ReplyView.vue")
  },
  {
    path: '/CollectionView',
    name: "CollectionView",
    component: () => import("../views/CollectionView.vue")
  },
  {
    path: '/MyPost',
    name: "MyPost",
    component: () => import("../views/MyPost.vue")
  },
  {
    path: '/HistoryView',
    name: "HistoryView",
    component: () => import("../views/HistoryView.vue")
  },
  {
    path: '/PersonList',
    name: "PersonList",
    component: () => import("../views/PersonList.vue")
  },
  {
    path: '/UserInfos',
    name: "UserInfos",
    component: () => import("../views/UserInfos.vue")
  },
  {
    path: '/ProductView',
    name: "ProductView",
    component: () => import("../views/ProductView.vue"),
    meta: {/* 路由缓存 */ keep: true }
  },
  {
    path: '/SearchView',
    name: "SearchView",
    component: () => import("../views/SearchView.vue")
  },
  {
    path: '/GameDetail',
    name: "GameDetail",
    component: () => import("../views/GameDetail.vue")
  },
  {
    path: '/LoginView',
    name: "LoginView",
    component: () => import("../views/LoginView.vue")
  },
  {
    path: '/GameCategory',
    name: "GameCategory",
    component: () => import("../views/GameCategory.vue")
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name != 'LoginView') {
    let user_key = localStorage.getItem("user_key");
    if (!user_key) {
      next("/LoginView")
      return
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
